// pages/center/collection/collection.js
var { request } = require('../../utils/api.js')
var app = new getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navH: 0,
    isIphoneX: false,
    // path:api.path,
    content:[],
    isEditP:false,
    isEditL:false,
    isEditS:false,
    user: {}
  },
  editP(){
    this.setData({
      isEditP:!this.data.isEditP
    })
  },
  editL(){
    this.setData({
      isEditL:!this.data.isEditL
    })
  },
  editS(){
    this.setData({
      isEditS:!this.data.isEditS
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getCountHeight()
  },
  getCountHeight(){
    let isIphoneX = app.globalData.isIphoneX
    if (isIphoneX) {
      this.setData({
        isIphoneX
      })
    }
    this.setData({
      navH: app.globalData.navHeight
    })
  },
  getFav(){
    let params = {
      member_id:this.data.user.member_id,
      oper_type: 1
    }
    request('my_oper_record','POST',params).then(res => {
      if (res.data.code == 200) {
        res.data.data.goods.forEach(ele=>{
          ele.checked = false
        })
        res.data.data.cases.forEach(ele=>{
          ele.checked = false
        })
        this.setData({
          content: res.data.data
        })
      }
    })
  },
  bindchangeRadio(e){
    let index = e.currentTarget.dataset.index
    this.data.content.goods[index].checked = !this.data.content.goods[index].checked
    this.setData({
      content:this.data.content
    })
  },
  bindchangeRadio1(e){
    let index = e.currentTarget.dataset.index
    this.data.content.article_arr[index].checked = true
    this.setData({
      content:this.data.content
    })
  },
  bindchangeRadio2(e){
    let index = e.currentTarget.dataset.index
    this.data.content.cases[index].checked = !this.data.content.cases[index].checked
    this.setData({
      content:this.data.content
    })
  },
  del(){
    let ids1 =this.data.content.goods.map(ele=>{
      if(ele.checked){
        return ele.id
      }
    })
    let ids2 =this.data.content.cases.map(ele=>{
      if(ele.checked){
        return ele.id
      }
    })
    let params={
      member_id:this.data.user.member_id,
      oper_type: 1,
      id_str:([...ids1,...ids2]).join(',')
    }
    request('del_many','POST',params).then(res=>{
      if(res.data.code==200){
        this.getFav()
        this.setData({
          isEditP: false
        })
        wx.showToast({
          title: '删除成功',
          icon: 'none',
          duration: 2000
        })
      }
    })
  },
  clear(){
    // let user = api.cache("user")
    let params={
      member_id:this.data.user.member_id,
      oper_type: 1
    }
    request('onekey_cancel','POST',params).then(res=>{
      if(res.data.code==200){
        this.getFav()
        this.setData({
          isEditP: false
        })
        wx.showToast({
          title: '删除成功',
          icon: 'none',
          duration: 2000
        })
      }
    })
  },
  toDetail(e){
    if (e.currentTarget.dataset.item.model_id == 1) {
      let item = e.currentTarget.dataset.item.goods
      wx.navigateTo({
        url: `/pages/product/productInfo/productInfo?id=${item.id}&type=1`,
      })
    } else if (e.currentTarget.dataset.item.model_id == 2) {
      let item = e.currentTarget.dataset.item.cases
      wx.navigateTo({
        url: `/pages/case/case-info/case-info?id=${item.id}&type=1`,
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let userInfo = wx.getStorageSync('user')
    if (userInfo && userInfo.id) {
      this.setData({
        user: userInfo
      })
    }
    this.getFav()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      // imageUrl: '../../../imgs/index/share.jpg',
      // title: app.globalData.shareTitle,
      // path: '/pages/index/index'
    }
  }
})